<!DOCTYPE html>
<html lang="zh">

<head>
    <meta charset="utf-8">
    <title>系部信息添加页面</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="format-detection" content="telephone=no">
    <link rel="stylesheet" href="./css/x-admin.css" media="all">
</head>

<body>
<div class="x-body">
    <form class="layui-form">
        <div class="layui-form-item">
            <label for="dno" class="layui-form-label">
                <span class="x-red">*</span>系部编号
            </label>
            <div class="layui-input-inline">
                <input type="text" id="dno" name="dno" required="" lay-verify="check_dno"
                       autocomplete="off" class="layui-input">
            </div>
            <div class="layui-form-mid layui-word-aux">
                <span class="x-red">*</span>不能重复,以d开头,长度小于等于3
            </div>
        </div>

        <div class="layui-form-item">
            <label for="dname" class="layui-form-label">
                <span class="x-red">*</span>系部名称
            </label>
            <div class="layui-input-inline">
                <input type="text" id="dname" name="dname" required="" lay-verify="check_dname"
                       autocomplete="off" class="layui-input">
            </div>
        </div>

        <div class="layui-form-item">
            <label for="tel" class="layui-form-label">
                <span class="x-red">*</span>联系电话
            </label>
            <div class="layui-input-inline">
                <input type="text" id="tel" name="tel" required="" autocomplete="off" class="layui-input"
                       lay-verify="check_tel">
            </div>
        </div>

        <div class="layui-form-item">
            <label for="add" class="layui-form-label">
            </label>
            <button id="add" class="layui-btn" lay-filter="add" lay-submit="">
                增加
            </button>
        </div>
    </form>
</div>
<script src="./lib/layui/layui.js" charset="utf-8"></script>
<script src="./js/x-layui.js" charset="utf-8"></script>
<script>
    layui.use(['form', 'layer'], function () {
        $ = layui.jquery;
        var form = layui.form()
            , layer = layui.layer;

        //自定义验证规则
        form.verify({
            nikename: function (value) {
                if (value.length < 5) {
                    return '昵称至少得5个字符啊';
                }
            },
            check_dno: function (value) {
                if (value.length < 1) {
                    return '系部编号不能为空';
                }
                if (value.length > 3) {
                    return '系部编号长度不能大于3';
                }
                if (!value.match(/^d[0-9]{1,3}$/)) {
                    return '系部编号必须以d开头,后面跟2位数字';
                }
            },
            check_dname: function (value) {
                if (value.length < 1) {
                    return '系部名称不能为空';
                }
            },
            check_tel: function (value) {
                if (value.length < 1) {
                    return '联系电话不能为空';
                }
            }
        });

        //监听提交
        form.on('submit(add)', function (data) {
            layer.confirm('确定添加吗？', {icon: 3, title: '提示添加'}, function (index) {
                $.ajax({
                    url: "/dep_add",
                    type: "post",
                    data: data.field,
                    success: function (data) {
                        data = JSON.parse(data);
                        if (data.code === 200) {
                            layer.msg("添加成功!", {icon: 1, time: 1000}, function () {
                                parent.layer.close(index);
                                parent.location.reload();
                            });
                        } else {
                            layer.msg(data.msg, {icon: 2, time: 1000});
                        }
                    }
                });

            });
            return false;
        });
    });
</script>
<script>
    var _hmt = _hmt || [];
    (function () {
        var hm = document.createElement("script");
        hm.src = "https://hm.baidu.com/hm.js?b393d153aeb26b46e9431fabaf0f6190";
        var s = document.getElementsByTagName("script")[0];
        s.parentNode.insertBefore(hm, s);
    })();
</script>
</body>

</html>